9l0p<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <router-link :to="'/property/survey'" class="link-type">
          <el-button icon="el-icon-back" size="mini">
            返 回
          </el-button>
        </router-link>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['module:question:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['module:question:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" :loading="exportLoading" @click="handleExport"
                   v-hasPermi="['module:question:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="questionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="调查主题" align="center" prop="questionTitle" />
      <el-table-column label="题目" align="center" prop="question" />
      <!--      <el-table-column label="选项内容" align="center" prop="answerSection" />-->
      <el-table-column label="问题类型" align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.questionType === 0">选择项</span>
          <span v-if="scope.row.questionType === 1">输入项</span>
          <span v-if="scope.row.questionType === 2">图片</span>
        </template>
      </el-table-column>
      <el-table-column label="排序索引" align="center" prop="sort" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['module:question:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['module:question:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList" />

    <!-- 添加或修改问卷调查问题对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="调查内容" prop="question">
              <el-input v-model="form.question" type="textarea" placeholder="请输入调查内容" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="排序索引" prop="sort">
              <el-input v-model="form.sort" type="number" placeholder="排序索引" size="mini"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态" prop="state">
              <el-radio-group v-model="form.state" size="mini">
                <el-radio :value="0" :label="0">正常</el-radio>
                <el-radio :value="1" :label="1">停用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="14">
            <el-form-item label="问题类型" prop="questionType">
              <el-radio-group v-model="form.questionType" @change="changeQuestionType" size="mini">
                <el-radio :value="0" :label="0">选择项</el-radio>
                <el-radio :value="1" :label="1">输入项</el-radio>
                <el-radio :value="2" :label="2">图片</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="图片数量" prop="maxImgCount" v-if="form.questionType == 2">
              <el-input v-model="form.maxImgCount" type="number" placeholder="最多可上传张图" size="mini"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item v-for="(domain, index) in sections"
                          :label="'选项' + (index + 1)"
                          :key="domain.key"
                          :prop="domain.value" v-if="form.questionType === 0">
              <el-input v-model="domain.value" :rules="{ required: true, message: '选项不能为空', trigger: 'blur' }" size="mini" style="margin: 5px;width: 60%">
                <template slot="append">
                  <el-button @click.prevent="removeSection(domain)" size="mini">删除</el-button>
                </template>
              </el-input>
              <el-checkbox v-model="domain.is_except" size="mini" border @change="(val) => changeException(val, domain,index)">是否异常项</el-checkbox>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="submitForm">确 定</el-button>
        <el-button @click="addSection" size="mini">新增选项</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listQuestion,
  getQuestion,
  delQuestion,
  addQuestion,
  updateQuestion,
  exportQuestion
} from "@/api/module/survey/question";
import { listPropertyNoPage } from "@/api/module/property/property";
import Template from "../wx/template";
import {exportPlan} from "@/api/module/event/plan";

export default {
  name: "Question",
  components: {Template},
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 问卷调查问题表格数据
      questionList: [],
      propertyListOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        propertyId: undefined,
        surveyId: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        questionTitle: [
          { required: true, message: "调查主题不能为空", trigger: "blur" }
        ],
        question: [
          { required: true, message: "调查内容不能为空", trigger: "blur" }
        ],
      },
      sections: [
        { value: '正常',check:'',is_except:'0'},
        { value: '异常',check:'',is_except:'1'},
        // { value: '基本满意',check:''},
        // { value: '不满意',check:''},
        // { value: '很不满意',check:''}
      ],
    };
  },
  created() {
    this.queryParams.surveyId = this.$route.query.surveyId ? this.$route.query.surveyId : 0;
    this.getList();
    this.getPropertyList();
  },
  methods: {
    /** 查询问卷调查问题列表 */
    getList() {
      this.loading = true;
      listQuestion(this.queryParams).then(response => {
        this.questionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getPropertyList() {
      listPropertyNoPage().then(response => {
        this.propertyListOptions = response.data;
      });
    },
    addSection() {
      this.sections.push({value: '', check: '',is_except:''});
    },
    changeQuestionType(val){
      if(val === 0){
        this.sections = [
          { value: '正常',check:'',is_except:''},
          { value: '异常',check:'',is_except:'true'},
          // { value: '基本满意',check:''},
          // { value: '不满意',check:''},
          // { value: '很不满意',check:''}
        ]
      }else{
        this.sections = undefined;
      }
    },
    removeSection(item) {
      let index = this.sections.indexOf(item);
      if (index !== -1) {
        this.sections.splice(index, 1)
      }
    },
    changeException(val,item,index){
      console.log('changeException',val)
      console.log('changeException',item)
      console.log('changeException',index)

      this.sections[index].is_except = val

      console.log('this.sections',this.sections)

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        propertyId: undefined,
        surveyId: this.queryParams.surveyId,
        questionTitle: undefined,
        question: undefined,
        answerSection: undefined,
        questionType:undefined,
        maxImgCount:undefined,
        state: undefined,
        createTime: undefined,
        sort:undefined,
        sections:undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加问卷调查问题";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getQuestion(id).then(response => {
        console.log('getQuestion ',response.data)
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改问卷调查问题";
        this.sections =  this.form.sections;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          console.log('this.sections',this.sections);
          if(this.sections != undefined && this.sections.length > 0){
            this.form.answerSection = JSON.stringify(this.sections);
          }

          if (this.form.id != null) {
            updateQuestion(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addQuestion(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除问卷调查问题编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delQuestion(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出问题数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportQuestion(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
